package com.ebt.m.customer.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.adapter.AdapterActiveCustomerPager;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.widget.SlidingTabLayout;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 最近30天活动客户activity
 *
 * @author Rick.Wang
 *         只保留“全部”
 */
public class ActivityCustomerActive extends FragmentActivity implements ViewPager.OnPageChangeListener {
    //view
    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;
    private TextView mTitleView;
    private AdapterActiveCustomerPager mAdapter;
    private List<Fragment> mFragments = new ArrayList<>();
    private ArrayList<String> mTitles = new ArrayList<String>();
    //data
    private int mViewPagerIndex;
    private Intent mIntentGet;
    private int from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();
        Bundle bundle = getIntent().getExtras();
        from = bundle.getInt("from");
        initViews();
        initData();
    }

    private void initWindow() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    public void initViews() {
        setContentView(R.layout.activity_customer_active);
        RxView.clicks(findViewById(R.id.back))
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    onBackPressed();
                });
        mTitleView = (TextView) this.findViewById(R.id.tv_title);
        mSlidingTabLayout = (SlidingTabLayout) this.findViewById(R.id.sliding_tab_layout);
        mViewPager = (ViewPager) this.findViewById(R.id.view_pager);

        mTitles.add("全部");
//        mTitles.add("计划书");
//        mTitles.add("笔记");
//        mTitles.add("打电话");

        mFragments.add(FragmentActiveThirty.newInstance(from));
//        mFragments.add(ActiveProposalFragment.newInstance());
//        mFragments.add(ActiveNoteFragment.newInstance());
//        mFragments.add(ActivePhoneFragment.newInstance());

        mAdapter = new AdapterActiveCustomerPager(getSupportFragmentManager(), mTitles, mFragments);
        mViewPager.setOffscreenPageLimit(4);
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(this);

        mSlidingTabLayout.setDistributeEvenly(true);
        mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.colorDefaultGreen));
        mSlidingTabLayout.setBackgroundColor(getResources().getColor(R.color.white));
        mSlidingTabLayout.setViewPager(mViewPager);
    }

    private void initData() {
        mIntentGet = getIntent();
        if (mIntentGet != null) {
            mViewPagerIndex = mIntentGet.getIntExtra(CustomerConstant.CustomerActive.VIEW_PAGER_INDEX, 0);
        }
        mTitleView.setText(CustomerConstant.CustomerActive.TITLES[mViewPagerIndex]);
        mViewPager.setCurrentItem(mViewPagerIndex, false);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mTitleView.setText(CustomerConstant.CustomerActive.TITLES[position]);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
